﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace TinyERP.Framework.Config
{
    /// <summary>
    /// Application node is presented for a UI feature in a module
    /// </summary>
    public class ApplicationNodeElement
    {
        public string ID { get; set; }
        
        public string Title { get; set; }
        public string Description { get; set; }
        public string UIClassName { get; set; }
        public string IconKey { get; set; }
        public string ResourceKey { get; set; }
        public string ModuleID { get; set; }
        public string FlowTo { get; set; }
        public IList<ApplicationNodeElement> Children { get; set; }
       

        
        public ApplicationNodeElement()
        {
            Children = new List<ApplicationNodeElement>();
        }
        public static ApplicationNodeElement ReadNodeInfoFromXml(XmlNode formNodeXml)
        {
            ApplicationNodeElement formNode = new ApplicationNodeElement();
            formNode.ID = formNodeXml.Attributes["id"].Value;
            formNode.Title = formNodeXml.Attributes["title"].Value;
        
            formNode.ModuleID = formNodeXml.Attributes["module"].Value;
            formNode.ResourceKey = formNodeXml.Attributes["resource-key"].Value;
            formNode.IconKey = formNodeXml.Attributes["icon"] == null ? null : formNodeXml.Attributes["icon"].Value;
            formNode.FlowTo = formNodeXml.Attributes["flow-to"] == null ? null : formNodeXml.Attributes["flow-to"].Value;
            if (formNodeXml.Attributes["uiclass"] != null)
            {
                formNode.UIClassName = formNodeXml.Attributes["uiclass"].Value;
            }


            XmlNode descriptionNodeXml = formNodeXml.SelectSingleNode("description");
            if (descriptionNodeXml != null)
            {
                formNode.Description = descriptionNodeXml.InnerText;
            }
            XmlNode childrenNodeXml = formNodeXml.SelectSingleNode("children");
            if (childrenNodeXml != null && childrenNodeXml.ChildNodes != null && childrenNodeXml.ChildNodes.Count > 0)
            {
                foreach (XmlNode childFormNodeXml in childrenNodeXml.ChildNodes)
                {
                    ApplicationNodeElement childNode = ReadNodeInfoFromXml(childFormNodeXml);
                    if (childNode != null)
                    {
                        formNode.Children.Add(childNode);
                    }
                }
            }
            return formNode;
        }
    }
}
